//
//  MJTStatusRetweetView.swift
//  swiftWeiBo
//
//  Created by MJT on 16/7/13.
//  Copyright © 2016年 MJT. All rights reserved.
//

import UIKit
import SnapKit

class MJTStatusRetweetView: UIView {
    //转发微博视图的底部约束
    var retweetViewBottom: Constraint?
    //持有viewMOdel
    var statusViewModel: MJTStatusViewModel? {
        didSet {
//            //设置转发微博内容
//            contentLabel.text = statusViewModel?.retweetContent
            //设置富文本
            contentLabel.attributedText = statusViewModel?.retweetAttributedString
//            卸载上次约束
            retweetViewBottom?.uninstall()
            if let count = statusViewModel?.status?.retweeted_status?.pic_urls?.count where count > 0 {
                pictureView.picURLs = statusViewModel?.status?.retweeted_status?.pic_urls

                //更新约束
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
                })
                pictureView.hidden = false
            } else {
                //隐藏配图
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    self.retweetViewBottom = make.bottom.equalTo(contentLabel).offset(HomeCellMargin).constraint
                })
            }
        }
    }
    //懒加载控件
    private lazy var contentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 14)
        label.numberOfLines = 0
        return label
    }()
    //加载pictureView
    private lazy var pictureView: MJTStatusPictureView1 = {
        let collectionView = MJTStatusPictureView1()
        collectionView.backgroundColor = self.backgroundColor
        return collectionView
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        contentLabel.backgroundColor = UIColor(white: 0.95, alpha: 1)
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        //设置约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self).offset(HomeCellMargin)
            make.leading.equalTo(self).offset(HomeCellMargin)
            make.trailing.equalTo(self).offset(-HomeCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HomeCellMargin)
            //根据图片个数计算大小
//            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        //关键约束
        self.snp_makeConstraints { (make) -> Void in
            self.retweetViewBottom = make.bottom.equalTo(pictureView).offset(HomeCellMargin).constraint
        }
    }
}
